﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockholmsStad.Web.UI.Class.Controller;
using StockholmsStad.Web.UI.Class.Facade;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.Class.Model;
using StockholmsStad.Web.UI.Class.Utilities;

namespace StockholmsStad.Web.UI.Registrering
{
    /// <summary>
    /// Websida för registreringen.
    /// </summary>
    public partial class Registration : Page
    {
        private RegistrationController _controller;

        /// <summary>
        /// Körs när sidan laddas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            Registration3.ViewController = new ViewController(MultiView1, View1, View2);
            _controller = new RegistrationController(new RegistrationModel(new HttpSessionAdapter(Session),
                                                                           new WebServiceFacade()), Registration2,
                                                     Registration3, Registration4);
            Registration2.Controller = _controller;
            if (!Page.IsPostBack)
            {
                _controller.ResetRegistrationTicketId();
            }
            StepControlView1.Clear();
            StepControlView1.AddStep("Personuppgifter", true, false);
            StepControlView1.AddStep("Kontaktuppgifter", false, false);
            StepControlView1.AddStep("Bekräfta uppgifter", false, false);
            StepControlView1.Refresh();
        }

        /// <summary>
        /// Knapptryck på 'nästa' vid registrering av personer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitRegistrationPersons_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                _controller.SubmitRegistrationPersons(Registration1.Person1, Registration1.Person2);
                MultiView1.SetActiveView(View2);
                StepControlView2.Clear();
                StepControlView2.AddStep("Personuppgifter", false, true);
                StepControlView2.AddStep("Kontaktuppgifter", true, false);
                StepControlView2.AddStep("Bekräfta uppgifter", false, false);
                StepControlView2.Refresh();
            }
        }

        /// <summary>
        /// Kanpptryck på 'nästa' vid registrering av kontaktperson och ceremoni
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitRegistrationContact_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                _controller.SubmitRegistrationContact(Registration2.Contact, Registration2.CeremonyInformation,
                                                      Registration2.ContactPersonListIsFirstPerson());
                MultiView1.SetActiveView(View3);

                StepControlView3.Clear();
                StepControlView3.AddStep("Personuppgifter", false, true);
                StepControlView3.AddStep("Kontaktuppgifter", false, true);
                StepControlView3.AddStep("Bekräfta uppgifter", true, false);
                StepControlView3.Refresh();
            }
        }

        /// <summary>
        /// Knapptryck på bekräfta registrering
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitRegistrationSummary_Click(object sender, EventArgs e)
        {
            _controller.SubmitRegistrationSummary();
            MultiView1.SetActiveView(View4);
        }

        /// <summary>
        /// Rensar formuläret
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnClearPersons_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Registrering/Registrering.aspx");
        }

        /// <summary>
        /// Användaren väljer att avbryta registreringen.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAbortRegistration_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }

        #region Nested type: ViewController

        private class ViewController : IViewController
        {
            private readonly MultiView multiView;
            private readonly View view1;
            private readonly View view2;

            public ViewController(MultiView multiView, View view1, View view2)
            {
                this.multiView = multiView;
                this.view1 = view1;
                this.view2 = view2;
            }

            #region IViewController Members

            public void ChangeToPerson1View()
            {
                multiView.SetActiveView(view1);
            }

            public void ChangeToPerson2View()
            {
                multiView.SetActiveView(view1);
            }

            public void ChangeToContactView()
            {
                multiView.SetActiveView(view2);
            }

            public void ChangeToCeremonyView()
            {
                multiView.SetActiveView(view2);
            }

            #endregion
        }

        #endregion
    }
}